from django.shortcuts import *
# Create your views here.
from django.views import View
from django.http import HttpResponseNotFound
from home.models import ArticleCategory,Article,Comment
from django.core.paginator import Paginator,EmptyPage



#主页展示
class IndexView(View):
    """首页广告"""

    def get(self, request):
        """提供首页广告界面"""
        #?cat_id=xxx&page_num=xxx&page_size=xxx
        cat_id=request.GET.get('cat_id',1)
        page_num = request.GET.get('page_num', 1)
        page_size = request.GET.get('page_size', 10)
        #判断分类id
        try:
            c = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
            return HttpResponseNotFound('没有此分类')

        # 获取博客分类信息
        categories = ArticleCategory.objects.all()

        #分页数据
        articles = Article.objects.filter(
            category=c    #
        )

        # 创建分页器：每页N条记录
        paginator = Paginator(articles, page_size)
        # 获取每页商品数据
        try:
            page_articles = paginator.page(page_num)
        except EmptyPage:
            # 如果没有分页数据，默认给用户404
            return HttpResponseNotFound('empty page')
        # 获取列表页总页数
        total_page = paginator.num_pages

        context = {
            'categories':categories,
            'category':c,
            'articles': page_articles,
            'page_size': page_size,
            'total_page': total_page,
            'page_num': page_num,
        }
        print(total_page)
        return render(request, 'index.html',context=context)

from django.views import View

# 详情页面
class DetailView(View):

    def get(self,request):
        # detail/?id=xxx&page_num=xxx&page_size=xxx
        #获取文档id
        id=request.GET.get('id')

        # 获取博客分类信息
        categories = ArticleCategory.objects.all()

        try:
            article=Article.objects.get(id=id)
        except Article.DoesNotExist:
            return render(request,'404.html')
        else:
            article.total_views +=1
            article.save()

        # 获取热点数据
        hot_articles = Article.objects.order_by('-total_views')[:9]
        comm = Comment.objects.filter(article=article).order_by('-created_time')

        context = {
            'categories':categories,
            'category':article.category,
            'article':article,
            'hot_articles':hot_articles,
            'comms':comm,
        }

        return render(request,'detail.html',context=context)

    def post(self,request):
        # 获取用户信息
        user = request.user
        # 判断用户是否登录
        if user and user.is_authenticated:
            # 接收数据
            rev_id = request.POST.get('rev_id')
            rev_content = request.POST.get('rev_content')
        else:
            return HttpResponseNotFound('没有此文章')

        # 判断文章是否存在
        try:
            article = Article.objects.get(id=rev_id)
        except Article.DoesNotExist:
            return HttpResponseNotFound('没有此文章')

        # 保存评论数据
        from home.models import Comment
        Comment.objects.create(content=rev_content, article=article, user=user)

        # 修改评论数量
        article.comments_count += 1
        article.save()

        # 刷新当前页面
        req_url = request.path + '?id=' + rev_id
        return redirect(req_url)
#
# #发表评论实现
# class DetailView(View):
#     def get(self,request):
#         #获取用户信息
#         user = request.user
#
#         #判断用户是否登录
#         if user and user.is_authenticated:
#             #接收数据
#             id = request.POST.get('id')
#             content = request.POST.get('content')
#
#         #判断文章是否存在
#         try:
#             article = Article.objects.get(id=id)
#         except Article.DoesNotExist:
#             return HttpResponseNotFound('没有此文章')
